package topo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import topo.console.ConsoleTextArea;
import topo.data.NodeDevice;
import topo.service.TopoDataManager;
import topo.sourcecode.LineNumber;
import topo.sourcecode.SourceCodeList;
import topo.sourcecode.SourceCodeTextArea;

import com.isnmp.common.snmp.SNMPFactory;

public class MainFrame extends JApplet {
    public static Frame frame;
    public static TopoDataSource source;
    public static TopoGraphView topoView;
    public TopoTreeView treeView;
    public JPanel sourceCodePanel = null;

    public JSplitPane rootSplitPane = null;
    public JSplitPane rightSplitPane = null;

    public JScrollPane treeScrollPane = null;
    public JScrollPane consoleScrollPane = null;
    public JScrollPane topoScrollPane = null;
    public JScrollPane sourceCodeScrollPane = null;
    public JScrollPane sourceCodeListScrollPane = null;

    public JTabbedPane topoAndSourceCodeTabbedPane = null;
    public JTabbedPane consoleTabbedPane = null;

    public static JTextArea consoleArea = null;

    public static JTextArea sourceCodeArea = null;

    public static JList sourceCodeList = null;

    public static MainFrame mainFrame = null;

    public static JMenuBar menuBar = new JMenuBar();

    public void init() {
        mainFrame = this;

        topoInit();
        try {
            mainFrameInit();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            SNMPFactory.init(1, null, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        frame = new Frame();
        frame.setBounds(getBounds());
    }

    public void mainFrameInit() throws Exception {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(screenSize.width, screenSize.height);

        this.treeScrollPane = new JScrollPane(this.treeView);

        this.topoScrollPane = new JScrollPane(topoView);

        sourceCodeList = new SourceCodeList();
        sourceCodeList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                String selectName = MainFrame.sourceCodeList.getSelectedValue().toString();
                try {
                    MainFrame.sourceCodeArea.setText(SourceCodeList.readSourceCodeFromFile(selectName));
                } catch (Exception e1) {
                    MainFrame.sourceCodeArea.setText("\"" + selectName + "\"的源文件不存在");
                    e1.printStackTrace();
                }
                MainFrame.sourceCodeArea.setCaretPosition(0);
            }
        });
        this.sourceCodeListScrollPane = new JScrollPane(sourceCodeList);

        sourceCodeArea = new SourceCodeTextArea();
        this.sourceCodeScrollPane = new JScrollPane(sourceCodeArea);

        LineNumber lineNumber = new LineNumber(sourceCodeArea);
        this.sourceCodeScrollPane.setRowHeaderView(lineNumber);

        this.sourceCodePanel = new JPanel();
        this.sourceCodePanel.setLayout(new BorderLayout());
        this.sourceCodePanel.add(this.sourceCodeListScrollPane, "West");
        this.sourceCodePanel.add(this.sourceCodeScrollPane, "Center");

        this.topoAndSourceCodeTabbedPane = new JTabbedPane();
        this.topoAndSourceCodeTabbedPane.add("设备视图", this.topoScrollPane);
        this.topoAndSourceCodeTabbedPane.add("核心源码", this.sourceCodePanel);

        consoleArea = new ConsoleTextArea();

        this.consoleScrollPane = new JScrollPane(consoleArea);
        this.consoleTabbedPane = new JTabbedPane();
        this.consoleTabbedPane.add("控制台", this.consoleScrollPane);

        this.rightSplitPane = new JSplitPane(0);
        this.rightSplitPane.setDividerLocation(screenSize.height / 2);
        this.rightSplitPane.setOneTouchExpandable(true);
        this.rightSplitPane.add(this.topoAndSourceCodeTabbedPane, "top");
        this.rightSplitPane.add(this.consoleTabbedPane, "bottom");

        this.rootSplitPane = new JSplitPane(1);
        this.rootSplitPane.setDividerLocation(screenSize.width / 5);
        this.rootSplitPane.setOneTouchExpandable(true);
        this.rootSplitPane.add(this.treeScrollPane, "left");
        this.rootSplitPane.add(this.rightSplitPane, "right");

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(this.rootSplitPane, "Center");
    }

    public void topoInit() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            source = new TopoDataSource();
            source.beginChange();

            topoView = new TopoGraphView(source);

            topoView.getStatusLabel().setText("状态栏");

            TopoMouseAction menuHandle = new TopoMouseAction();
            topoView.setMouseHandle(menuHandle);

            this.treeView = new TopoTreeView(source);

            this.treeView.syncTopoview(topoView);

            addDefaultNode();

            source.commitChange();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void addDefaultNode() {
        NodeDevice node = new NodeDevice();
        node.setIpAddress("127.0.0.1");
        node.setReadCommit("public");
        node.setWriteCommit("public");
        node.setNodeType("计算机");
        node.setDisplayName("默认设备");
        node.setSnmpPort(161);
        node.setSnmpVersion(1);
        node.setIsEnableSNMPProxy(0);
        node.setSnmpProxyIP("127.0.0.1");
        node.setPoseX(50);
        node.setPoseY(50);
        node.setNodeName(node.genKey());

        TopoDataManager.getInstance().addNode(node);
    }

    public static void main(String[] args) {
        MainFrame mainFrame1 = new MainFrame();
        mainFrame1.init();

        JFrame frame = new JFrame("SNMP功能演示 ");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(screenSize.width, screenSize.height);
        frame.setDefaultCloseOperation(3);
        frame.setContentPane(mainFrame.getContentPane());
        frame.setJMenuBar(menuBar);
        MenuBarManager.initMenu(menuBar, topoView);
        frame.setVisible(true);
    }
}